package com.version1.diabeco;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.app.Activity;
import android.app.AlarmManager;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.version1.diabeco.service.AlarmaInicializador;

public class ActivityAyuda extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_ayuda);

		File fileBrochure = new File(Environment.getExternalStorageDirectory().getPath()
				+ "/PreguntasFrecuentesDiabeCo.pdf");
		if (!fileBrochure.exists()) {
			CopyAssetsbrochure();
		}

		/** PDF reader code */
		File file = new File(Environment.getExternalStorageDirectory().getPath() + "/PreguntasFrecuentesDiabeCo.pdf");

		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setDataAndType(Uri.fromFile(file), "application/pdf");
		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		try {
			getApplicationContext().startActivity(intent);
		} catch (ActivityNotFoundException e) {
			// Toast.makeText(SecondActivity.this, "NO Pdf Viewer", Toast.LENGTH_SHORT).show();
		}
	}

	// method to write the PDFs file to sd card
	private void CopyAssetsbrochure() {
		AssetManager assetManager = getAssets();
		String[] files = null;
		try {
			files = assetManager.list("");
		} catch (IOException e) {
			Log.e("tag", e.getMessage());
		}
		for (int i = 0; i < files.length; i++) {
			String fStr = files[i];
			if (fStr.equalsIgnoreCase("PreguntasFrecuentesDiabeCo.pdf")) {
				InputStream in = null;
				OutputStream out = null;
				try {
					in = assetManager.open(files[i]);
					out = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/" + files[i]);
					copyFile(in, out);
					in.close();
					in = null;
					out.flush();
					out.close();
					out = null;
					break;
				} catch (Exception e) {
					Log.e("tag", e.getMessage());
				}
			}
		}
	}

	private void copyFile(InputStream in, OutputStream out) throws IOException {
		byte[] buffer = new byte[1024];
		int read;
		while ((read = in.read(buffer)) != -1) {
			out.write(buffer, 0, read);
		}
	}

	// WebView webview = (WebView)findViewById(R.id.webview);
	// webview.loadUrl("www.google.com.ar");

	/*
	 * 
	 * public void truncateTables(View view) { ApplicationDaoImpl.getInstance(this).truncate(); }
	 * 
	 * public void addFixedData(View view) { ApplicationDaoImpl.getInstance(this).addFixedData(); }
	 * 
	 * public void showComida(View view) { ApplicationDaoImpl.getInstance(this).showComida(); }
	 * 
	 * public void showDetalleComida(View view) { ApplicationDaoImpl.getInstance(this).showDetalleComida(); }
	 * 
	 * public void showVerticesGeocerca(View view) { ApplicationDaoImpl.getInstance(this).showVerticesGeocerca(); }
	 * public void sendSMS(View view) { sendSMS("+541130324573", "DiabeCo dice Hola!!!"); } private void sendSMS(String
	 * phoneNumber, String message) { SmsManager sms = SmsManager.getDefault(); sms.sendTextMessage(phoneNumber, null,
	 * message, null, null); }
	 */

}
